package com.wang.gulimallgeteway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

//项目全局跨域配置
@Configuration
public class CorsComfig {

    //本质是一个filter，拦截全部的url，对这些url配置上可跨域的响应头
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //1,配置跨域
        //全类型的请求头可以跨域
        corsConfiguration.addAllowedHeader("*");
        //全类型的请求方法可以跨域
        corsConfiguration.addAllowedMethod("*");
        //全部的域名可以跨域
        corsConfiguration.addAllowedOrigin("*");
        //跨域时携带cookie
        corsConfiguration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsWebFilter(source);
    }
}
